#include "BinaryTree.h"

BinaryTree::BinaryTree()
{
    root = nullptr;
}

BinaryTree::~BinaryTree()
{
    Free(root);
}

void BinaryTree::Insert(const ElementType &element)
{
    root = InsertNode(root, element);
}

void BinaryTree::Remove(const ElementType &element)
{
    root = RemoveNode(root, element);
}

BinaryTree::ElementType *BinaryTree::Find(const ElementType &element)
{
    return FindNode(root, element);
}

BinaryTree::TreeNode *BinaryTree::InsertNode(TreeNode *node, const ElementType &element)
{
    if (node == nullptr)  
    {
        return new TreeNode(element);   // 
    }
    if (node->data == element)  // 二叉树不能存在重复元素 不插入重复元素
    {
        return node;
    }
    else if (node->data > element)  // 当前结点比要插入是数大
    {
        node->left = InsertNode(node->left, element);  // 递归到左子树
        // 左边重
        if (LRDevition(node) > 1)
        {
            // 左左
            if (node->left->data > element)
            {
                // 右旋
                node = RotateRight(node);
            }
            else
            // 左右失衡
            {
                // 左右旋
                node = RotateLeftRight(node);
            }
        }
    }
    else
    {
        node->right = InsertNode(node->right, element);  // 递归到右子树
        // 右边重
        if (LRDevition(node) < -1)
        {
            // 右右
            if (node->right->data < element)
            {
                node = RotateLeft(node);
            }
            else
            // 右左
            {
                node = RotateRightLeft(node);
            }
        }
    }
    return node;
}

void BinaryTree::show()
{
    std::cout << "前序遍历:";
    PrevTravel(root);
    std::cout << std::endl;

    std::cout << "中序遍历:";
    MidTravel(root);
    std::cout << std::endl;

    std::cout << "后序遍历:";
    PostTravel(root);
    std::cout << std::endl;

    std::cout << "层序遍历:";
    LinkQueue q;
    q.Push(root);
    while (!q.IsEmpty())
    {
        TreeNode *node = (TreeNode *)*q.GetFront();
        q.Pop();
        std::cout << node->data << " ";
        if (node->left)
            q.Push(node->left);
        if (node->right)
            q.Push(node->right);
    }
    std::cout << std::endl;
}

void BinaryTree::PrevTravel(TreeNode *node)
{
    if (node == nullptr)
        return;
    std::cout << node->data << " ";
    PrevTravel(node->left);
    PrevTravel(node->right);
}

void BinaryTree::MidTravel(TreeNode *node)  
{
    if (node == nullptr)
        return;
    MidTravel(node->left);
    std::cout << node->data << " ";      // 什么时候执行这个打印，满足return条件的时候吗
    MidTravel(node->right);
}

void BinaryTree::PostTravel(TreeNode *node)   // 每当递归调用返回时，它会返回到调用该递归函数的节点，也就是当前正在处理的节点。
{
    if (node == nullptr)   
        return;
    PostTravel(node->left);
    PostTravel(node->right);
    std::cout << node->data << " ";   // 左子树和右子树都被遍历完之后
}

void BinaryTree::Free(TreeNode *node)
{
    if (node == nullptr)   // 结点为空，直接返回
        return;
    Free(node->left);  // 释放左子树(左孩子)
    Free(node->right); // 释放右子树(右孩子)
    delete node;       // 释放当前结点
}

BinaryTree::TreeNode *BinaryTree::RemoveNode(TreeNode *node, const ElementType &element)
{
    if (node == nullptr)   // 当前结点为空
        return nullptr;
    if (node->data < element)   // 从根开始找 ，要删的大，向右找 
    {
        node->right = RemoveNode(node->right, element);
    }
    else if (node->data > element)  // 要删的小，往左找
    {
        node->left = RemoveNode(node->left, element);
    }
    else    // 相等，开始删，分三种情况
    {
        // 叶子
        if (node->left == nullptr && node->right == nullptr)  // 没有子树，直接删
        {
            delete node;
            return nullptr;
        }
        else if (node->left && node->right)  // 左右子树都存在
        {
            // 取右子树中最小结点
            TreeNode *minNode = GetMinNode(node->right);
            node->data = minNode->data;
            node->right = RemoveNode(node->right, minNode->data);
        }
        // 有一个孩子
        else                             // 存在一个子树
        {
            TreeNode *child = node->left ? node->left : node->right;
            delete node;
            return child;
        }
    }
    // 左边重
    if (LRDevition(node) > 1)
    {
        // 左左
        if (LRDevition(node->left) > 0)
        {
            // 右旋
            node = RotateRight(node);
        }
        else
        // 左右失衡
        {
            // 左右旋
            node = RotateLeftRight(node);
        }
    }
    if (LRDevition(node) < -1)
    {
        // 右右
        if (LRDevition(node->right) < 0)
        {
            node = RotateLeft(node);
        }
        else
        // 右左
        {
            node = RotateRightLeft(node);
        }
    }
    return node;
}

BinaryTree::TreeNode *BinaryTree::GetMinNode(TreeNode *node)
{
    TreeNode *minNode = node;
    while (minNode->left)
        minNode = minNode->left;
    return minNode;
}

BinaryTree::ElementType *BinaryTree::FindNode(TreeNode *node, const ElementType &element)
{
    if (node == nullptr)
        return nullptr;
    if (node->data == element)
        return &node->data;
    else if (node->data > element)
        return FindNode(node->left, element);
    else
        return FindNode(node->right, element);
}

BinaryTree::TreeNode *BinaryTree::RotateRight(TreeNode *root)  // 右旋
{
    TreeNode *t = root->left;   // 把这个值保存下来
    root->left = t->right;
    t->right = root;
    return t;   // 返回新的根
}

BinaryTree::TreeNode *BinaryTree::RotateLeft(TreeNode *root)  // 左旋
{
    TreeNode *t = root->right;
    root->right = t->left;
    t->left = root;
    return t;   // 返回新的结点
}

BinaryTree::TreeNode *BinaryTree::RotateLeftRight(TreeNode *root)   // 左右旋  
{
    // 左子树左旋
    root->left = RotateLeft(root->left);
    return RotateRight(root); // 整棵树右旋  
}

BinaryTree::TreeNode *BinaryTree::RotateRightLeft(TreeNode *root)  // 
{
    // 右子树右旋
    root->right = RotateLeft(root->right);
    return RotateLeft(root); // 整棵树左旋
}

int BinaryTree::GetNodeHight(TreeNode *node)
{
    if (node == nullptr)
        return 0;
    int leftHight = GetNodeHight(node->left);
    int rightHight = GetNodeHight(node->right);
    return (leftHight > rightHight ? leftHight : rightHight) + 1;
}

int BinaryTree::LRDevition(TreeNode *node)   //左右是否平衡        
{
    if (node == nullptr)
        return 0;
    return GetNodeHight(node->left) - GetNodeHight(node->right);
}
